package com.yangjun.dao.impl.usermanage;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserManageDaoImpl {
	
	@Autowired
	@Resource(name = "bikadJdbcTemplate")
	private JdbcTemplate jdbc;
	
	/**
	 * 修改用户菜单访问状态
	 * @param userid
	 * @param menuid
	 * @param value
	 */
	public boolean updateUserMenuStatus(Integer userid,Integer menuid,Integer value){
		
		
		System.err.println(userid+":"+menuid+":"+value);
		Object[] sz = {value,userid,menuid};
		String sql = "update DM_KAD_DATAPRD_USER_MENU set status=? ,updatetime=sysdate " 
				+ "where userid=? and menu_id=?";
		int update = jdbc.update(sql,sz);
		if(update>0){
			return true;
		}
		
		return false;
	}
	
	
	
	/**
	 * 给用户添加菜单权限
	 * @param userid
	 * @param menuid
	 */
	public boolean addUserMenu(Integer userid,Integer menuid){
		Object[] sz = {userid,menuid};
		
//		System.err.println("userid--"+userid+"  menuid--"+menuid);
		String sql = "insert into DM_KAD_DATAPRD_USER_MENU (userid,Menu_Id,Createtime,Status) "
				+ "values (?,?,sysdate,1)";
		System.out.println(sql);
		
		int update = jdbc.update(sql, sz);
		
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 重置密码
	 * @param userid
	 * @param password
	 * @return
	 */
	public boolean updateUserPassword(Integer userid,String password){
		if(StringUtils.isBlank(password)){
			password="360kad";
		}
		Object[] sz = {password,userid};
		String sql = "update DM_KAD_DATAPRD_USER set password=?,udpatetime=sysdate where userid=?";
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 修改用户名密码
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean updatePassword(String username,String oldpassword,String newpassword){
		Object[] sz = {newpassword,username,oldpassword};
		String sql = "update DM_KAD_DATAPRD_USER set password=?,udpatetime=sysdate where username=? and password=?";
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 新增用户
	 * @param username
	 * @param realname
	 * @return
	 */
	public boolean addUser(String username,String realname){
		Object[] sz = {username,realname};
		String sql = "insert into DM_KAD_DATAPRD_USER (USERNAME,REALNAME,PASSWORD,CREATETIME,Status) "
				+ "values (?,?,'360kad',sysdate,1) ";
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * 修改用户
	 * @param userid
	 * @param username
	 * @param realname
	 * @return
	 */
	public boolean updateUser(Integer userid,String username,String realname){
	
		Object[] sz = {username,realname,userid};
		String sql = "update DM_KAD_DATAPRD_USER set username=?,realname=?,udpatetime=sysdate where userid=?";
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断用户名是否存在
	 * @param username
	 * @return
	 */
	public boolean isExistUser(String username){
		Object[] sz = {username};
		String sql = "select USERNAME from DM_KAD_DATAPRD_USER T Where T.USERNAME=? ";
		List<Map<String, Object>> queryForList = jdbc.queryForList(sql, sz);
		
		if(queryForList!=null && queryForList.size()>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 修改用户状态
	 * @param userid
	 * @param value
	 * @return
	 */
	public boolean updateUserStatus(Integer userid,Integer value){
		Object[] sz = {value,userid};
		String sql = "update DM_KAD_DATAPRD_USER set status=? ,UDPATETIME=sysdate " 
				+ "where userid=?";
		int update = jdbc.update(sql,sz);
		if(update>0){
			return true;
		}
		
		return false;
	}
	

	/**
	 * 给用户添加日志
	 * @param username
	 * @return
	 */
	public boolean addUserLog(String username,String ip,String url){
		Object[] sz = {username,ip,url};
		
		String sql = "insert into DV_USER_LOG (username,last_visit_date,ip,url) "
				+ "values (?,sysdate,?,?)";
		System.out.println(sql);
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * 给用户添加按钮事件日志
	 * @param username
	 * @return
	 */
	public boolean addOnclickUserLog(String username,String onclick_type,String ip,String visit_url){
		Object[] sz = {username,onclick_type,ip,visit_url};
		
		String sql = "insert into DV_USER_ONCLICK_LOG (username,onclick_type,ip,visit_date,visit_url) "
				+ "values (?,?,?,sysdate,?)";
		System.out.println(sql);
		int update = jdbc.update(sql, sz);
		if(update>0){
			return true;
		}
		
		return false;
	}

	
	/**
	 * 今天是否已经存在log
	 * @param username
	 * @return
	 */
	public boolean isExistLog(String username,String ip){
		Object[] sz = {username,ip};
		String sql = "select * from DV_USER_LOG where username=? and ip=? and trunc(last_visit_date)=trunc(sysdate)";
		List<Map<String, Object>> queryForList = jdbc.queryForList(sql, sz);
		if(queryForList!=null && queryForList.size()>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 删除用户log
	 * @param username
	 * @return
	 */
	public void deleteUserLog(String username,String ip){
		Object[] sz = {username,ip};
		String sql = "delete from DV_USER_LOG where username=? and ip=? and trunc(last_visit_date)=trunc(sysdate)";
		jdbc.update(sql, sz);
	}
}
